import 'package:flutter/material.dart';

/// Flutter code sample for [DropdownMenu].

var list = <(int, String)>[(0, ''), (1, 'One'), (2, 'Two'), (3, 'Three'), (4, 'Four')];

void main() => runApp(const DropdownMenuApp());

class DropdownMenuApp extends StatefulWidget {
  const DropdownMenuApp({super.key});

  @override
  State<DropdownMenuApp> createState() => _DropdownMenuApp();
}

class _DropdownMenuApp extends State<DropdownMenuApp> {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      theme: ThemeData(useMaterial3: true),
      home: Scaffold(
        appBar: AppBar(title: const Text('DropdownMenu Sample')),
        body: const Center(
          child: DropdownMenuExample(),
        ),
      ),
    );
  }
}

class DropdownMenuExample extends StatefulWidget {
  const DropdownMenuExample({super.key});

  @override
  State<DropdownMenuExample> createState() => _DropdownMenuExampleState();
}

class _DropdownMenuExampleState extends State<DropdownMenuExample> {
  // String dropdownValue = list.first;
  (int, String) defaultValue = list.first;

  @override
  Widget build(BuildContext context) {
    return Column(
      children: [
        DropdownMenu<(int, String)>(
          initialSelection: defaultValue,
          onSelected: (value) {
            // This is called when the user selects an item.
            setState(() {
              // dropdownValue = value!;
            });
          },
          dropdownMenuEntries: list.map((value) {
            return DropdownMenuEntry<(int, String)>(value: value, label: value.$2);
          }).toList(),
        ),
        TextButton(
          onPressed: () {
            setState(() {
              defaultValue = (1, 'One');
            });
          },
          child: const Text('更新初始值'),
        ),
      ],
    );
  }
}
